package lesson2;

import junit.framework.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.*;

/**
 * Created by Worker31 on 17.03.14.
 */
public class HomeWork2 {
    final String filename = "C:/test.txt";

    @BeforeTest
    public void setUp() throws Exception {
        File testFile = new File(filename);
        if (!testFile.exists()) {
            testFile.createNewFile();
        }
        PrintWriter out = new PrintWriter(testFile.getAbsoluteFile());
        try {
            out.println("gshndgfvhsd 10 sdkhfjsdkf");
            out.println("hjhhhereeh 1 hhhdhgf");
            out.println("oiyuuiyhoiuyui 90 kugkjygjkygy");
        } finally {
            out.close();
        }
    }

    @Test
    public void testingTest2() throws IOException {
        FileReader fileReader = new FileReader(filename);
        BufferedReader in = new BufferedReader(fileReader);

        int[] array = new int[3];
        String line = null;
        int i = 0;
        int sortUp = 0;
        final int ExpectedTestResult = 0;

        while ((line = in.readLine()) != null) {
            array[i] =Integer.parseInt( line.substring(line.indexOf("\u0020")+1, line.lastIndexOf("\u0020")) );
            i++;
        }
        in.close();

        for (int j = 0; j < 2; j++){
            if (array[j] > array[j+1]){
                sortUp++;
            }
        }

        Assert.assertEquals("\nString is not sorted by integer element\n",ExpectedTestResult, sortUp );
    }

    @AfterTest
    public void setDown() {
        File testFile = new File(filename);

        if (testFile.exists()) {
            testFile.delete();
        }  else {
            System.out.println("File is not deleted");
        }
    }
}
